/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.sqlgen.builders;

import java.util.Iterator;

import org.eaicompiler.model.Action;
import org.eaicompiler.model.Event;
import org.eaicompiler.model.ScriptDefinition;

/**
 * Builder for EventAI scripts SQL
 * 
 * @author floop.pl
 */
public class EventaiScriptsSQLBuilder extends SQLBuilder {
	private ScriptDefinition	script;
	private StringBuilder		sql;
	private boolean				sqlBuilt	= false;
	private int					creatureId;
	private boolean				useDelete;

	public EventaiScriptsSQLBuilder(ScriptDefinition script, boolean useDelete, int creatureId) {
		super();
		this.useDelete = useDelete;
		this.script = script;
		this.creatureId = creatureId;
		this.sql = new StringBuilder();
	}

	/**
	 * @param useDelete
	 *            the useDelete to set
	 */
	public void setUseDelete(boolean useDelete) {
		this.useDelete = useDelete;
	}

	public String buildSQLForCreature() {
		if (!sqlBuilt) {
			// Generate proper deletion script
			if (useDelete) {
				sql.append("delete from `eventai_scripts` where `creature_id`='");
				sql.append(creatureId);
				sql.append("';\n");
			}
			sql.append("insert into `eventai_scripts`(`creature_id`,`event_chance`,`event_type`,`event_inverse_phase_mask`,`event_param1`,`event_param2`,`event_param3`,`action1_type`,`action1_param1`,`action1_param2`,`action1_param3`,`action2_type`,`action2_param1`,`action2_param2`,`action2_param3`,`action3_type`,`action3_param1`,`action3_param2`,`action3_param3`,`comment`) values\n");
			Iterator<Event> i = script.getEvents().iterator();
			while(i.hasNext())
			{
				Event e= i.next();
				sql.append("\t('");
				sql.append(creatureId);
				sql.append("','");
				sql.append(e.getChance());
				sql.append("','");
				sql.append(e.getType());
				sql.append("','");
				sql.append(e.getInversePhaseMask());
				sql.append("',");
				for(int f=0;f<Event.MAX_EVENT_PARAMS;f++)
				{					
					sql.append("'");
					sql.append(e.getParam(f));
					sql.append("'");
					if(f<Event.MAX_EVENT_PARAMS-1)
					{
						sql.append(",");						
					}
				}
				sql.append(",");						
				for(int f=0;f<Event.MAX_EVENT_ACTIONS;f++)
				{
					Action a = e.getAction(f);
					if(a==null)
					{
						// Make sure we have any action
						a = new Action();
					}
					a = a.getActionForCreature(creatureId);
					sql.append("'");
					sql.append(a.getType());
					sql.append("',");
					for(int z=0;z<Action.MAX_ACTION_PARAMS;z++)
					{
						sql.append("'");
						sql.append(a.getParam(z));
						sql.append("'");
						if(z<Action.MAX_ACTION_PARAMS-1)
						{
							sql.append(",");
						}
					}
					if(f<Event.MAX_EVENT_ACTIONS-1)
					{
						sql.append(",");
					}
				}
				sql.append(",'");
				if(e.getComment()!=null)
				{
					sql.append(escapeString(e.getComment()));
				}
				sql.append("')");
				if(i.hasNext())
				{
					sql.append(",\n");
				}
			}
			sql.append(";\n");
			sqlBuilt = true;
		}
		return sql.toString();
	}

}
